package com.wss.module.wan.ui.wxnumber.mvp;

import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.ui.wxnumber.mvp.contract.WXArticleContract;
import com.wss.module.wan.ui.wxnumber.mvp.model.WXArticleModel;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Describe：微信文章控制器
 * Created by 吴天强 on 2020/8/13.
 */
public class WXArticlePresenter extends BasePresenter<WXArticleModel, WXArticleContract.View>
        implements WXArticleContract.Presenter {
    @Override
    protected WXArticleModel createModule() {
        return new WXArticleModel(getLifecycleOwner());
    }

    @Override
    public void start() {
        getArticle();
    }

    @Override
    public void getArticle() {
        final int wxId = getView().getWxId();
        final int page = getView().getPage();
        if (wxId == -1) {
            return;
        }
        System.out.println("==== get article wx id ； " + wxId + " , page ； " + page);
        showLoading();
        getModel().getWxArticle(wxId, getView().getPage(), new CallBack<String>() {


            @Override
            public void onStart() {
            }

            @Override
            public void onCompleted() {
                dismissLoading();
            }

            @Override
            public void onError(ApiException e) {
                getView().onError("", e.getMessage());
                dismissLoading();
            }

            @Override
            public void onSuccess(String s) {
//                dismissLoading();
                String data = JsonUtils.getString(s, "data");
                List<Article> datas = JsonUtils.getList(JsonUtils.getString(data, "datas"), Article.class);
                if (ValidUtils.isValid(datas)) {
                    getView().refreshWxArticle(datas);
                } else {
                    getView().onEmpty("暂无数据");
                }
            }
        });
    }
}
